package com.taxicall.taxi;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;

import com.taxicall.taxi.NetworkService.LocalBinder;

import taxicall.entities.NetworkMessage;
import taxicall.entities.Taxi;
import taxicall.entities.TaxiRoute;
import taxicall.entities.NetworkMessage.MessageType;
import udptransport.SerializationUtils;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.ServiceConnection;
import android.content.DialogInterface.OnClickListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Toast;

public class PassengerApproval extends TaxiNetworkActivity {

	protected ServiceConnection serviceConnection = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName name) {
			bound = false;
		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder iBinder) {
			LocalBinder binder = (LocalBinder) iBinder;
			service = binder.getService();
			bound = true;
			new PassengerRequestHandler().execute(null, null, null);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_client_selection);

		Bundle bundle = getIntent().getExtras();
		serverIp = (InetAddress) bundle.get("serverIp");
		thisTaxi = (Taxi) bundle.get("thisTaxi");
	}

	private class PassengerRequestHandler extends
			AsyncTask<Integer, Integer, Integer> {

		@Override
		protected Integer doInBackground(Integer... params) {
			if (!(service != null && bound && network != null)) {
				throw new RuntimeException("Service or network not initialized");
			}

			while (true) {
				DatagramPacket packet = null;
				try {
					packet = network.receive();
				} catch (IOException e) {
					e.printStackTrace();
				}

				// indicates error in network
				if (packet == null){
					Toast.makeText(getApplicationContext(), "Network error", Toast.LENGTH_LONG).show();
					return null;
				}
				
				NetworkMessage msg = (NetworkMessage) SerializationUtils.deserialize(packet.getData());
				TaxiRoute clientRoute = (TaxiRoute) msg.getDataObject();
				showDialog(clientRoute, packet);
			}
		}
	}
	
	private void showDialog(TaxiRoute clientRoute, final DatagramPacket requestPacket){
		AlertDialog.Builder dialog = new Builder(getApplicationContext());
		dialog.setCancelable(false);
		dialog.setTitle("Client request");
		dialog.setMessage("Client wants to go from " );
		dialog.setPositiveButton("Yes", new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				NetworkMessage msg = new NetworkMessage(MessageType.RideReply, "yes");
				try {
					network.send(msg, requestPacket.getAddress(), requestPacket.getPort());
				} catch (IOException e) {
					Toast.makeText(getApplicationContext(), "Couldnt send reply to client.", Toast.LENGTH_LONG).show();
					e.printStackTrace();
				}
			}
		});
		
		dialog.setNegativeButton("No", new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				NetworkMessage msg = new NetworkMessage(MessageType.RideReply, "no");
				try {
					network.send(msg, requestPacket.getAddress(), requestPacket.getPort());
				} catch (IOException e) {
					Toast.makeText(getApplicationContext(), "Couldnt send reply to client.", Toast.LENGTH_LONG).show();
					e.printStackTrace();
				}
			}
		});
		dialog.create();
		dialog.show();
	}
}
